package exttable.timetable.models;

import utils.Localisation;

import javax.swing.table.AbstractTableModel;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Apr 11, 2010
 * Time: 7:05:41 PM
 * To change this template use File | Settings | File Templates.
 */
public class AnalysisTableModel extends AbstractTableModel {
    public static final int CARRIED = 0;
    public static final int ID = 1;
    public static final int NAME = 2;
    public static final int VERSION = 3;

    private Vector<AnalysisModel> data;
    private String[] columns = {Localisation.getMessage("timetable.analysiscarried")
            , Localisation.getMessage("timetable.analysid")
            , Localisation.getMessage("timetable.analysname")
            , Localisation.getMessage("form.version")
    };

    public AnalysisTableModel(Vector<AnalysisModel> data) {
        this.data = data;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        AnalysisModel am = data.get(rowIndex);
        switch (columnIndex) {
            case CARRIED:
                return am.isCarried();
            case ID:
                return am.getAnalysis().getAnalysisId();
            case NAME:
                return am.toString();
            case VERSION:
                return am.getVersion().toString();
            default:
                throw new IllegalArgumentException();
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case CARRIED:
                return Boolean.class;
            case ID:
                return Integer.class;
            case NAME:
                return String.class;
            case VERSION:
                return String.class;
            default:
                throw new IllegalArgumentException();
        }
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 0;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        AnalysisModel am = data.get(rowIndex);
        if (columnIndex == 0) {
            am.setCarried(Boolean.valueOf(aValue.toString()));
            fireTableCellUpdated(rowIndex, columnIndex);
        }
    }
}
